//
//  SISSGuaranteeCompanySearchViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/10/31.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSGuaranteeCompanySearchViewController.h"
#import "SISSGuaranteeCompanyTypeInViewController.h"

@interface SISSGuaranteeCompanySearchViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) UITextField *companyNameTextField;
@property (weak, nonatomic) UIButton *confirmButton;
/// tableView
@property (weak, nonatomic) SISSBaseTableView *tableView;
@property (weak, nonatomic) UIButton *noResultButton;

/// <#Annotation#>
@property (assign, nonatomic) NSInteger count;
@end

@implementation SISSGuaranteeCompanySearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"担保公司";
    
    [self requestData];
}

- (void)requestData {
    self.count = 0;
    self.noResultButton.hidden = self.count != 0;
    self.tableView.hidden = self.count == 0;
}

- (void)confirmButtonClickAction {
    
    SISSGuaranteeCompanyTypeInViewController *vc = [[SISSGuaranteeCompanyTypeInViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)noResultButtonClickAction {
    SISSGuaranteeCompanyTypeInViewController *vc = [[SISSGuaranteeCompanyTypeInViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseID = @"ID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)setupSubviews {
    [super setupSubviews];
    
    UITextField *companyNameTextField = [[UITextField alloc] init];
    companyNameTextField.placeholder = @"请输入担保公司名称";
    companyNameTextField.layer.borderWidth = 1;
    companyNameTextField.layer.borderColor = UIColor.groupTableViewBackgroundColor.CGColor;
    [self.view addSubview:companyNameTextField];
    self.companyNameTextField = companyNameTextField;
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    confirmButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [confirmButton setBackgroundColor:MAIN_COLOR];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.layer.cornerRadius = 4;
    confirmButton.layer.masksToBounds = YES;
    [self.view addSubview:confirmButton];
    self.confirmButton = confirmButton;
    
    SISSBaseTableView *tableView = [[SISSBaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    // 自动行高
    tableView.estimatedRowHeight = 80;
    tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
//    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@""];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    UIButton *noResultButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [noResultButton setTitle:@"没有找到该担保公司？" forState:UIControlStateNormal];
    noResultButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [noResultButton setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
    [noResultButton setBackgroundImage:[ImageWithColor imageWithColor:[UIColor whiteColor]] forState: UIControlStateNormal];
    [noResultButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [noResultButton setBackgroundImage:[ImageWithColor imageWithColor:MAIN_COLOR] forState: UIControlStateHighlighted];
    [noResultButton addTarget:self action:@selector(noResultButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    noResultButton.layer.cornerRadius = 4;
    noResultButton.layer.masksToBounds = YES;
    [self.view addSubview:noResultButton];
    self.noResultButton = noResultButton;
}

- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    
    [self.companyNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view).offset(2 * globelMargin);
        make.top.equalTo(weakSelf.mas_topLayoutGuideBottom).offset(2 * globelMargin);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.companyNameTextField.mas_right).offset(globelMargin);
        make.top.equalTo(weakSelf.companyNameTextField);
        make.bottom.equalTo(weakSelf.companyNameTextField);
        make.right.equalTo(weakSelf.view).offset(-globelMargin);
        make.width.mas_equalTo(10 * globelMargin);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.companyNameTextField.mas_bottom).offset(globelMargin);
        make.left.right.equalTo(weakSelf.view);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(weakSelf.view);
        }
    }];
    
    [self.noResultButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view);
        make.centerY.equalTo(weakSelf.view).offset(- SCREEN_HEIGHT * 0.1);
        make.height.mas_equalTo(5.5 * globelMargin);
        make.width.mas_equalTo(SCREEN_WIDTH * 0.7);
    }];
}

@end
